package java8.date;

import org.junit.Test;

import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

public class DateTest {

    //Instant实例表示时间线上的一个点。 参考点是标准的Java纪元(epoch)，即1970-01-01T00：00：00Z（1970年1月1日00:00 GMT）
    @Test
    public void instant() throws Exception {
        //记录开始时间和结束时间
        Instant start = Instant.now();
        // do something here
        Thread.sleep(1000);
        Instant end = Instant.now();
        System.out.println(Duration.between(start, end).toMillis());
    }

    //LocalDate类只包括日期没有时间的部分。 它也没有时区。
    //LocalDate是不可变的，任何返回LocalDate的方法都返回LocalDate的新实例。
    @Test
    public void localDate() throws Exception {
        LocalDate today = LocalDate.now();
        LocalDate tomorrow = today.plusDays(1);
        LocalDate oneDecadeAgo = today.minus(1, ChronoUnit.DECADES);

        System.out.println("Day of month: " + today.getDayOfMonth());
        System.out.println("Today is " + today);
        System.out.println("Tomorrow is " + tomorrow);
        System.out.println("A decade ago was " + oneDecadeAgo);
        System.out.println("Year : " + today.get(ChronoField.YEAR));
        System.out.println("Day of year:" + today.getDayOfYear());
    }

    //Period类基于日期的时间数量构建
    @Test
    public void period() throws Exception {
        LocalDate dateA = LocalDate.of(1978, 8, 26);
        LocalDate dateB = LocalDate.of(1988, 9, 28);
        Period period = Period.between(dateA, dateB);
        System.out.printf("Between %s and %s"
                        + " there are %d years, %d months"
                        + " and %d days%n", dateA, dateB,
                period.getYears(),
                period.getMonths(),
                period.getDays());
    }

    //LocalDateTime类是一个没有时区的日期时间的构建
    @Test
    public void localDateTime() throws Exception {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime endOfYear = LocalDateTime.of(2015, 12, 31, 8, 0);
        now = LocalDateTime.now();
        //明天同一个时间
        LocalDateTime sameTimeTomorrow = now.plusHours(24);
        //基于时区的构建
        ZonedDateTime parisTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
    }

    //Duration类是基于时间的持续时间的构建。 它与Period类似
    //不同之处在于Duration的时间分量为纳秒精度，并考虑了ZonedDateTime实例之间的时区。 下表显示了Duration中重要的方法。
    //表示时间量或两个日期之间的差
    @Test
    public void duration() throws Exception {

        LocalDateTime dateTimeA = LocalDateTime
                .of(2015, 1, 26, 8, 10, 0, 0);
        LocalDateTime dateTimeB = LocalDateTime
                .of(2015, 1, 26, 11, 40, 0, 0);
        Duration duration = Duration.between(
                dateTimeA, dateTimeB);
        System.out.printf("There are %d hours and %d minutes.%n",
                duration.toHours(),
                duration.toMinutes() % 60);

        //考虑时区
        ZonedDateTime zdt1 = ZonedDateTime.of(
                LocalDateTime.of(2015, Month.JANUARY, 1, 8, 0),
                ZoneId.of("America/Denver"));
        ZonedDateTime zdt2 = ZonedDateTime.of(
                LocalDateTime.of(2015, Month.JANUARY, 1, 8, 0),
                ZoneId.of("America/Toronto"));

        duration = Duration.between(zdt1, zdt2);
        System.out.printf("There are %d hours and %d minutes.%n",
                duration.toHours(),
                duration.toMinutes() % 60);
    }

    @Test
    public void duration3() throws Exception {
        char a=31;
        System.out.println(a);
        char b=30;
        System.out.println(b);
    }

    @Test
    public void duration4() throws Exception {
        Integer.parseInt("1.2");
    }
}
